﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SurveyListsAdministration
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class SurveyListEventHandler : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (properties.List.BaseTemplate == SPListTemplateType.Survey)
            {
                using (DisabledEventFiringScope scope = new DisabledEventFiringScope())
                {
                    base.ItemAdded(properties);
                    SPTools.CheckSurveyItem(properties);
                }
            }
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            if (properties.List.BaseTemplate == SPListTemplateType.Survey)
            {
                using (DisabledEventFiringScope scope = new DisabledEventFiringScope())
                {
                    base.ItemUpdated(properties);
                    SPTools.CheckSurveyItem(properties);
                }
            }
        }

        public override void ItemDeleting(SPItemEventProperties properties)
        {
            if (properties.List.BaseTemplate == SPListTemplateType.Survey)
            {
                using (DisabledEventFiringScope scope = new DisabledEventFiringScope())
                {
                    base.ItemDeleting(properties);
                    SPTools.DeleteSurveyAdministrationItem(properties);
                }
            }
        }
    }
}
